<?php
/**
 * 会员
 */
namespace App;
use Model\LouieModuleMod;
use Model\MemberMod;
use Model\WqMemberMod;
use Pub\tool\Setting as SettingCache;

defined('IN_IA') or exit('Access Denied');

class Member extends Action
{
    private $Member = null;

    private $_uid = 0;

    public function __construct()
    {
        parent::__construct();
        if(isset($this->_gpc["uid"])) $this->_uid = $this->_gpc["uid"];
        $this->Member = new MemberMod();
    }

    /**
     * 注册,用户已关注公众号注册，未关注公众号不做任何操作
     * @param uid 用户id
     */
    public function register()
    {
        $_member = $this->Member->findOne(array("uniacid"=>$this->_uniacid,"uid"=>$this->_uid),array("id"));
        if(!$_member) {
            $_data = array("uid" => $this->_uid, "uniacid" => $this->_uniacid);
            if ($this->Member->insert($_data)) {
                $this->_result = array("status" => "success");
            }
        }
        $this->result($this->_result);
    }

    /**
     * 获取用户信息
     * @param uid 用户id
     */
    public function getMember()
    {
        if(substr(SettingCache::get(SettingCache::SUB),0,1) > 0){
            $SignAction = new SignIn();
            $SignAction->missingSign($this->_uid);
        }
        $_member = array(
            "balance"=>$this->getBalance($this->_uid)
        );
        if($_member) $this->_result = array("status"=>"success","data"=>$_member);
        $this->result($this->_result);
    }

    /**
     * 后台列表总数
     */
    public function getTotal()
    {
        $this->_result = array(
            "status"=>"success",
            "data"=>$this->Member->total(
                array("uniacid"=>$this->_uniacid)
            )
        );
        $this->result($this->_result);
    }

    /**
     * 后台列表
     */
    public function getList()
    {
        $_list = $this->Member->findAll(
            array("uniacid"=>$this->_uniacid),
            array(
                "limit"=>$this->_gpc["limit"],
                "order"=>array("id","desc")
            ),
            array("uid")
        );
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $_list[$_k]["balance"] = $this->getBalance($_v["uid"]);
                $_member = $this->uidToInfo($_v["uid"]);
                $_list[$_k]["nickname"] = $_member["nickname"];
                $_list[$_k]["header"] = $_member["avatar"];
            }
            unset($_member);
            $this->_result = array("status"=>"success","data"=>$_list);
        }
        $this->result($this->_result);
    }

}